﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Admin;
using Services.WXProjectAdmin;
using Xproject.Util;
using System.Text;
using System.IO;
using Model.Util;

namespace WeiXinpProAdmin.Controllers
{
    public class AdvertisementController: AdminBaseController
    {
        SystemAdServices m_SystemAdServices;

        public AdvertisementController(SystemAdServices mSystemAdServices)
        {
            m_SystemAdServices = mSystemAdServices;
        }

        #region ActionResult 
        public ActionResult SystemAdList()
        {
            return View();
        }

        public ActionResult SystemAdEdit(string Id)
        {
            Advertisement ad = null;
            if (!string.IsNullOrEmpty(Id))
            {
                ad = m_SystemAdServices.GetById(Site, Id);
            }            
            return View(ad);
        }
        #endregion

        #region 
        public JsonResult GetAdvertList(int limit, int offset, string adName, string keyWord, string adType, string remarks,
            DateTime? startTime, DateTime? endTime, string order, string ordername)
        {
            var jsonData = new Object();
            var lstRes = new List<Advertisement>();
            int total = 0;
            lstRes = m_SystemAdServices.GetAllAdvertisements(Site, limit, offset, adName, keyWord, adType, remarks, startTime, endTime, order, ordername, out total);           
            return Json(new { total = total, rows = lstRes }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult EditSystemAd(FormCollection collection)
        {
            var jsonData = new Object();
            string result = string.Empty;
            bool success = false;
            string Id = collection["ID"];
            Advertisement advertisement = new Advertisement();
            if (!string.IsNullOrEmpty(Id))
            {
                advertisement = m_SystemAdServices.GetById(Site, Id);
            }
            //else
            //{
            //    advertisement.DeadTime = DateTime.Now.AddMonths(6);
            //    advertisement.ID = Guid.NewGuid().ToString();
            //}

            HttpRequest request = System.Web.HttpContext.Current.Request;
            HttpFileCollection FileCollect = request.Files;
            UploadFile uf = null;
            if (FileCollect.Count > 0) //如果集合的数量大于0 
            {
                string Virbase = @"Upload\Images\Advertisement\";
                string DataTimeFolder = DateTime.Now.ToString("yyyyMMdd");
                string localpath = AppDomain.CurrentDomain.BaseDirectory + Virbase;
                string Folder = localpath + DataTimeFolder;
                if (!Directory.Exists(Folder))
                {
                    Directory.CreateDirectory(Folder);
                }
                HttpPostedFile FileSave = FileCollect[0]; //用key获取单个文件对象HttpPostedFile 
                uf = new UploadFile();
                uf.FileName = FileSave.FileName;
                uf.FileExt = Path.GetExtension(FileSave.FileName);
                uf.FileSize = FileSave.ContentLength;
                uf.Site = Site;
                uf.ServiceAddress = CommonHelper.GetAppSettings("Domain");
                string guidName = Guid.NewGuid().ToString() + uf.FileExt;
                uf.Path = ("\\" + Virbase + DataTimeFolder + "\\" + guidName).Replace("\\", "/");
                uf.FileId = Guid.NewGuid().ToString();
                string LocalFullPath = Folder + @"\" + guidName;
                uf.LocalFullPath = LocalFullPath;
                FileSave.SaveAs(LocalFullPath); //将上传的东西保存                                                           
                advertisement.AdPicUrl =  uf.Path;
                advertisement.AdDomain = uf.ServiceAddress;
             }                        

            advertisement.AdLink = collection["AdLink"];
            advertisement.AdName = collection["AdName"];             
            advertisement.AdSource = collection["AdSource"];
            advertisement.AdType = collection["AdType"];          
            advertisement.KeyWord = collection["KeyWord"];
            advertisement.Remarks = collection["Remarks"];
            advertisement.Site = Site;
            advertisement.SortId =int.Parse( collection["SortId"]);
            string enable = collection["Enable"];
            advertisement.Enable = int.Parse(string.IsNullOrEmpty(enable) ? "0" : ((enable.ToString() == "on") ? "1" : "0"));
            string IsShow = collection["IsShow"];
            advertisement.IsShow = int.Parse(string.IsNullOrEmpty(IsShow) ? "0" : ((enable.ToString() == "on") ? "1" : "0"));
            if (!string.IsNullOrEmpty(Id))
            {
                
                success = m_SystemAdServices.EditAdvertisement(uf, advertisement, this.CurrentUser.UserID.ToString());
            }
            else
            {
                advertisement.DeadTime = DateTime.Now.AddMonths(6);
                advertisement.ID = Guid.NewGuid().ToString();
                success = m_SystemAdServices.AddAdvertisement(uf, advertisement, this.CurrentUser.UserID.ToString());
            }
         
            result = success ? "编辑成功" : "编辑失败";
            jsonData = new { Success = success, Data = result };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DelAdvertisement(string Id)
        {
            var jsonData = new Object();
            string result = string.Empty;
            bool success = false;
          
            Advertisement advertisement = new Advertisement();
            if (!string.IsNullOrEmpty(Id))
            {
                advertisement = m_SystemAdServices.GetById(Site, Id);
                success= m_SystemAdServices.DelAdvertisement(advertisement);
            }
            result = success ? "删除成功" : "删除失败";
            jsonData = new { Success = success, Data = result };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
           
        }
        #endregion
    }
}